# -*- coding:utf-8 -*-
from . import db

TYPE_BRANCH = 0
TYPE_ASSESS = 1
TYPE_INSURANCE = 2

def addCompany(name, _type):
    return db.insert('company', name = name, type = _type)

def getById(companyId):
    result = db.query('SELECT * FROM company WHERE id = $companyId', locals())
    if result:
        return result[0]
    return None

def getBatchByType(type):
    result = db.query('SELECT * FROM company WHERE type = $type', locals())
    return list(result)

def getAllCompanys():
    result = db.query('SELECT * FROM company')
    return list(result)

def deleteById(companyId):
    return db.delete('company', 'id = $companyId', vars = locals())

def getCompanyByUserIds(userIds):
    userIds = set(userIds)
    userIds = ','.join([str(userId) for userId in userIds])
    return list(db.query('SELECT user.id as user_id, user.type as user_type, company.name as company_name, company.type as company_type FROM user LEFT JOIN company ON company.id = user.company_id WHERE user.id IN (%s)' % userIds))

def clear():
    db.query('delete from company')
